<?php
class LoginAction extends Action{
   public function index(){//登陆界面
      //如果已经登录
	  
	  if(!empty($_SESSION["admin"])) $this->redirect("Index/index");
      import("ORG.Cons");
	  $setup=Cons::$websetup;
	  $this->assign("setup",$setup);
	  $sysconfig=M("sysconfig");
	  $is_verycode=$sysconfig->where("id=1")->getField("is_verycode");
	  $this->assign("is_verycode",$is_verycode);
	  $this->display();
   }
   
   public function check(){//验证
      checkpost();
      $admin=$_POST["admin"];
	  $password=$_POST["password"];
	  $status=$_POST["status"];
	  $sysconfig=M("sysconfig");
	  $is_verycode=$sysconfig->where("id=1")->getField("is_verycode");
	  if($is_verycode=="开启"){
	  if($_SESSION['verify'] != md5($_POST['verycode'])) { 
                $this->error('验证码错误！'); 
      } 
    }
	  $setup=M("websetup");
	  $login_expire=(int)($setup->limit(1)->getField("login_expire"));
	  
	  $Admin=M("Admin");
	  $map["admin"]=$admin;
	  $map["password"]=$password;
	  if($user=$Admin->where($map)->find()){//如果成功
	       if($status==1){
	         ini_set("session.gc_maxlifetime",$login_expire);
		     setcookie(session_name(),session_id(),time()+$login_expire,"/");
		   }
	       $_SESSION["admin"]=$user["admin"];
		   //$_SESSION["pression"]=$user["pression"];
		   //记录日志
		   //获取用户名  ip地址(真实地址)  登陆时间 
		   //import("ORG.Net.IpLocation");
		   //$myIp=new IpLocation("QQWry.Dat");
		   
		   //$ip=$myIp->get_client_ip();
		   //$logintime=date("Y-m-d H:i:s");
		   //$username=$_SESSION["admin"];
		   //$l=$myIp->getlocation($ip);
		   //$address=$l["country"];
		   //if(IS_WIN) {
		     //$end="\r\n";
		   //}else{
		     //$end="\n";
		   //}
		   //$content="[".$logintime."]"."用户名：".$username."  "."ip地址：".$ip."(".iconv("gb2312","utf-8",$address).")".$end;
		   //$filepath="Admin_log/".$_SESSION["admin"].".txt";
		   //if(!file_exists($filepath)){
		     // Vendor("LJW.Inc_File");
			  //$myfile=new Inc_File;
			  //$myfile->createfile($filepath,true);
		   //}
		   //chmod($filepath,0777);
		   //if($fp=fopen($filepath,"ab+")){
		      //fwrite($fp,$content);
		   //}
		   $this->redirect("Index/index");
	  }else{
	       $this->error("登陆失败!");
	  }

   }
   public function verify(){
	   $type = isset($_GET['type'])?$_GET['type']:'gif'; 
       import("ORG.Util.Image"); 
	   //Image::showAdvVerify('png',180,40);  高级验证码
       Image::buildImageVerify(4,1,$type);
   }

}
?>
